package com.qbw.core.base;

import android.content.Context;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.qbw.core.holder.EmptyHolder;
import com.qbw.core.holder.LoadHolder;
import com.qbw.core.model.Empty;
import com.qbw.core.model.ViewType;
import com.qbw.core.view.RefreshLoadLayout;
import com.qbw.log.XLog;
import com.qbw.recyclerview.expandable.ExpandableAdapter;

/**
 * Created by Bond on 2017/02/08 16:34
 * you can contact me at qbaowei@qq.com
 */


public abstract class BaseAdapter extends ExpandableAdapter {

    protected Context mContext;

    protected Object mListener;

    private RefreshLoadLayout.OnLoadFailedListener mOnLoadFailedListener;
    private EmptyHolder.Listener mEmptyListener;

    public BaseAdapter(Context context) {
        mContext = context;
    }

    public BaseAdapter(Context context,
                       RefreshLoadLayout.OnLoadFailedListener onLoadFailedListener) {
        this(context);
        mOnLoadFailedListener = onLoadFailedListener;
    }

    public BaseAdapter(Context context,
                       RefreshLoadLayout.OnLoadFailedListener onLoadFailedListener,
                       EmptyHolder.Listener emptyListener) {
        mContext = context;
        mOnLoadFailedListener = onLoadFailedListener;
        mEmptyListener = emptyListener;
    }

    public BaseAdapter(Context context,
                       RefreshLoadLayout.OnLoadFailedListener onLoadFailedListener,
                       Object listener) {
        this(context, onLoadFailedListener);
        mListener = listener;
    }

    public BaseAdapter(Context context, Object listener) {
        mContext = context;
        mListener = listener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == getLoadViewType()) {
            return onCreateLoadHolder(parent);
        }
        if (viewType == getEmptyViewType()) {
            return onCreateEmptyHolder(parent);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof BaseHolder) {
            ((BaseHolder) holder).bindData(position, getItem(position));
        }
    }

    @Override
    public int getItemViewType(int position) {
        int vt = getItemViewType(getItem(position));
        return vt == -1 ? super.getItemViewType(position) : vt;
    }

    @Override
    public int getItemViewType(Object t) {
        if (t instanceof RefreshLoadLayout.Load) {
            if (XLog.isEnabled() && getLoadViewType() == -1)
                XLog.e("使用加载更多功能，必须再Adapter里面重写getLoadViewType方法，返回对应的viewType");
            return getLoadViewType();
        } else if (t instanceof ViewType) {
            return ((ViewType) t).getViewType();
        } else if (t instanceof Empty) {
            if (XLog.isEnabled() && getEmptyViewType() == -1)
                XLog.e("使用空视图功能，必须再Adapter里面重写getEmptyViewType方法，返回对应的viewType");
            return getEmptyViewType();
        }
        XLog.e("Class %s has no itemType", t.getClass().getName());
        return -1;
    }

    private LoadHolder onCreateLoadHolder(ViewGroup viewParent) {
        return new LoadHolder(mContext, viewParent, mOnLoadFailedListener);
    }

    private EmptyHolder onCreateEmptyHolder(ViewGroup viewGroup) {
        return new EmptyHolder(mContext, viewGroup, mEmptyListener);
    }

    /**
     * 显示加载更多布局
     */
    public void showLoad(int status) {
        int fc = getFooterCount();
        if (fc > 0) {
            int fp = findLoadPosition();
            if (fp != -1) {
                RefreshLoadLayout.Load load = (RefreshLoadLayout.Load) getFooter(fp);
                load.setStatus(status);
            } else {
                addFooter(new RefreshLoadLayout.Load(status));
            }
        } else {
            addFooter(new RefreshLoadLayout.Load(status));
        }
    }

    /**
     * 删除加载更多布局
     */
    public void removeLoad() {
        int fp = findLoadPosition();
        if (fp != -1) {
            removeFooter(fp);
        }
    }

    /**
     * 找到加载更多的位置
     */
    protected int findLoadPosition() {
        int fc = getFooterCount();
        int i;
        for (i = 0; i < fc; i++) {
            if (getItemViewType(getFooter(i)) == getLoadViewType()) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 加载更多布局的ViewType值
     */
    public int getLoadViewType() {
        return -1;
    }

    protected int findEmptyPosition() {
        int fc = getFooterCount();
        int i;
        for (i = 0; i < fc; i++) {
            if (getItemViewType(getFooter(i)) == getEmptyViewType()) {
                return i;
            }
        }
        return -1;
    }

    /**
     * 没有数据布局的viewType
     */
    public int getEmptyViewType() {
        return -1;
    }

    public void showEmpty(Empty empty) {
        int fc = getFooterCount();
        if (fc > 0) {
            int fp = findEmptyPosition();
            if (fp != -1) {
                updateFooter(fp, empty);
            } else {
                int loadPos = findLoadPosition();
                if (loadPos == -1) {
                    addFooter(empty);
                } else {
                    addFooter(loadPos, empty);
                }
            }
        } else {
            addFooter(empty);
        }
    }

    public void removeEmpty() {
        int fp = findEmptyPosition();
        if (fp != -1) {
            removeFooter(fp);
        }
    }
}
